package top.soft.windows;

import top.soft.bean.User;
import top.soft.util.CommonData;
import top.soft.util.NetWork;
import top.soft.util.Request;
import top.soft.util.Response;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @ClassName: UpdateView
 * @Author: Amosen
 * @Since: 2021/4/21
 */

// 这个类用于更新选项卡，给予修改密码的功能
public class UpdateView extends JPanel {

    private JLabel password, newPassword, rePassword;
    private JPasswordField passwordBar, newPasswordBar, rePasswordBar;
    private JButton confirmBtn, resetBtn;

    public UpdateView() {
        init();
    }

    public void init() {
        // 提示信息
        password = new JLabel(CommonData.RESET_PASSWORD);
        newPassword = new JLabel(CommonData.RESET_NEW_PASSWORD);
        rePassword = new JLabel(CommonData.RESET_RE_PASSWORD);

        // 输入框
        passwordBar = new JPasswordField(CommonData.BAR_COLUMNS);
        newPasswordBar = new JPasswordField(CommonData.BAR_COLUMNS);
        rePasswordBar = new JPasswordField(CommonData.BAR_COLUMNS);

        // 按钮
        confirmBtn = new JButton(CommonData.CONFIRM_BTN);
        resetBtn = new JButton(CommonData.RESET_BTN);

        // 添加事件
        confirmBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                doUpdate();
            }
        });

        resetBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                doReset();
            }
        });

        setLayout(new BorderLayout());

        JPanel panelCenter = new JPanel();
        panelCenter.setLayout(new GridLayout(3, 1));
        JPanel colOne = new JPanel();
        JPanel colTwo = new JPanel();
        JPanel colThree = new JPanel();
        colOne.setLayout(new FlowLayout());
        colTwo.setLayout(new FlowLayout());
        colThree.setLayout(new FlowLayout());

        colOne.add(password);
        colOne.add(passwordBar);
        colTwo.add(newPassword);
        colTwo.add(newPasswordBar);
        colThree.add(rePassword);
        colThree.add(rePasswordBar);

        panelCenter.add(colOne);
        panelCenter.add(colTwo);
        panelCenter.add(colThree);

        add(panelCenter, BorderLayout.CENTER);

        JPanel panelSouth = new JPanel();
        panelSouth.setLayout(new FlowLayout());
        panelSouth.add(confirmBtn);
        panelSouth.add(resetBtn);
        add(panelSouth, BorderLayout.SOUTH);
    }

    private boolean check() {
        char[] password = passwordBar.getPassword();
        char[] newPassword = newPasswordBar.getPassword();
        char[] rePassword = rePasswordBar.getPassword();
        if (password == null) {
            passwordBar.requestFocus();
            return false;
        }
        if (newPassword == null) {
            newPasswordBar.requestFocus();
            return false;
        }
        if (rePassword == null) {
            rePasswordBar.requestFocus();
            return false;
        }
        String strPassword = String.valueOf(password);
        String strNewPassword = String.valueOf(newPassword);
        String strRePassword = String.valueOf(rePassword);

        if ("".equals(strPassword) || strPassword.trim().length() == 0) {
            passwordBar.requestFocus();
            return false;
        }
        if ("".equals(strNewPassword) || strNewPassword.trim().length() == 0) {
            newPasswordBar.requestFocus();
            return false;
        }
        if ("".equals(strRePassword) || strRePassword.trim().length() == 0) {
            rePasswordBar.requestFocus();
            return false;
        }
        if (!strNewPassword.equals(strRePassword)) {
            JOptionPane.showMessageDialog(null, "两次输入的密码不一致！");
            return false;
        }
        return true;
    }

    private void doUpdate() {
        if (!check()) {
            return;
        } else {
            User loginUser = CommonData.getUser();
            if (loginUser == null) {
                return;
            } else {
                String account = loginUser.getAccount();
                String newPassword = String.valueOf(newPasswordBar.getPassword());
                // 封装为Request对象
                Request request = new Request(CommonData.UPDATE_PASSWORD_PATH, Request.GET);
                request.addData("account", account);
                request.addData("newPassword", newPassword);
                Response response = NetWork.service(request);
                if (response == null) {
                    JOptionPane.showMessageDialog(null, "网络连接失败！请检查后重试");
                } else {
                    if (response.getCode() != 200) {
                        JOptionPane.showMessageDialog(null, "密码修改失败，请检查后端程序");
                    } else {
                        JOptionPane.showMessageDialog(null, "密码修改成功！");
                    }
                }
            }
        }
    }

    // 重置控件
    private void doReset() {
        passwordBar.setText("");
        newPasswordBar.setText("");
        rePasswordBar.setText("");
    }
}
